package com.geek.novel.fragment

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Message
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.geek.novel.activity.base.BaseActivity
import com.geek.novel.event.EventObserver
import com.geek.novel.utils.LogCatUtil

abstract class BaseFragment : Fragment(), EventObserver, View.OnClickListener {

	private val logTag = "BaseFragment"
	
	lateinit var root:View
	
	abstract fun layoutId(): Int
	
	abstract fun pageName(): String

	abstract fun fragmentName(): String
	
	/**
	 * 一些变量的初始化
	 */
	abstract fun initV()

	/**处理观察者的事件*/
	override fun handleEvent(message: Message) {
	}
	
	/**
	 * 点击事件
	 */
	abstract fun myClick(v: View)
	fun setClickLsn(v: View){
		v.setOnClickListener(this)
	}
	
	
	override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
		root = inflater.inflate(layoutId(), container, false)
		LogCatUtil.d(logTag, "onCreateView")
		return root
	}
	
	override fun onActivityCreated(savedInstanceState: Bundle?) {
		super.onActivityCreated(savedInstanceState)
		LogCatUtil.d(logTag, "onActivityCreated")
		initV()
	}

	/**
	 * 点击事件的入口
	 */
	override fun onClick(v: View?) {
		try {
			this.myClick(v!!)
		}catch (e: Exception){
			e.printStackTrace()
		}
	}

	/**
	 * fragment隐藏的回调函数
	 */
	open fun onHide(){}
	/**
	 * fragment显示的回调函数
	 */
	open fun onShow(){}

	/**
	 * 界面跳转
	 */
	open fun startActivityByAnim(ctx: Context, clazz: Class<out Any>, params: Bundle?, code: Int=0,
								 animIn: Int=android.R.anim.fade_in, animExit: Int=android.R.anim.fade_out){
		val baseActivity = activity as BaseActivity
		val intent = baseActivity.parseIntent(ctx, clazz, params)
		if(code == 0){
			this.startActivity(intent)
		}else{
			this.startActivityForResult(intent, code)
		}
		baseActivity.overridePendingTransition(animIn, animExit)
	}
}